﻿using UnityEngine;
using UnityEngine.UI;
using Anderson.NetLobby.Server;

class DemoServer : MonoBehaviour
{

    public int port = 8000;
    public Button StartButton;
    public Button StopButton;
    public Button SendHeartButton;

    private NetLobbyServer server = null;

    private void Start()
    {
        StartButton.onClick.AddListener(OnStartServer);
        StopButton.onClick.AddListener(OnStopServer);
        SendHeartButton.onClick.AddListener(OnTest);

        StartButton.interactable = true;
        StopButton.interactable = false;
        SendHeartButton.interactable = false;
    }

    public void OnTest()
    {

    }

    public void OnStartServer()
    {
        StartButton.interactable = false;
        StopButton.interactable = true;
        SendHeartButton.interactable = true;

        if (null == server)
        {
            server = new NetLobbyServer(port, 500, 50, new UnityLogger());
        }
    }

    public void OnStopServer()
    {
        server.StopServer(() => {
            Debug.Log("Stop Finished");
            server = null;
        });

        StartButton.interactable = true;
        StopButton.interactable = false;
        SendHeartButton.interactable = false;
    }

    private void OnDestroy()
    {
        if (null != server)
        {
            server.StopServer(() => { Debug.Log("Stop Finished."); });
        }
    }
}
